/*=====================================================================
             为TradeDashboardDetails获取最后一次拜访的产品种类数和拜访总次数
             用于为报表的Average product line获取SKU数和统计Coverage的拜访次数；
    ==========================================================================*/


trigger addRecordForTradeDashboardDetails on In_Store_Visit__c (before insert,after update) {

boolean tf=false;
if(system.trigger.isupdate){
for(in_store_visit__c t:trigger.new){
    if(trigger.oldmap.get(t.id).sku_number__c!=t.sku_number__c){
        tf=true;
    }
}
}

if(system.trigger.isInsert||tf){
  list<TradeDashboardDetails__c> tddl=new list<TradeDashboardDetails__c>();
     list<TradeDashboardDetails__c> tinsert=new list<TradeDashboardDetails__c>();
     list<id> outlet=new list<id>();
     list<date> visitd=new list<date>();
     list<date> lastd=new list<date>();
     integer i;
     list<in_store_visit__c> storeTrigger=new list<in_store_visit__c>();
     for(in_store_visit__c t:trigger.new){
         storeTrigger.add(t);
     }
     
     for(In_Store_Visit__c t:storeTrigger){
         outlet.add(t.outletname_del__c);
     }
     for(In_Store_Visit__c t:storeTrigger){
         visitd.add(t.Visit_Date__c.toStartOfMonth());
     }
     
     tddl=[select id,ownerid,outlet__c,salesMonth__c,CriticalEnabler__c,ScoreStandard__c,target__c,Actual__c
              from TradeDashboardDetails__c 
              where ownerid=:userinfo.getuserid() and outlet__c in:outlet and SalesMonth__c in:visitd and (criticalEnabler__c=:'Coverage'or criticalEnabler__c=:'Average product line')];
     
     if(tddl.size()>0){
     integer num;
       for(i=0;i<storeTrigger.size();){
           in_store_visit__c t=storeTrigger[i];
           num=0;
              for(tradedashboarddetails__c td:tddl){
                 if(td.salesmonth__c==t.visit_date__c.tostartofmonth()&&td.outlet__c==t.OutletName_del__c){
                     if(td.criticalEnabler__c=='Coverage'){
                         if(system.trigger.isinsert){
                          for(in_store_visit__c t2:storeTrigger){
                                     if(t.outletName_del__c==t2.outletname_del__c && t.visit_date__c.month()-1==t2.visit_date__c.month()){
                                         td.actual__c=td.actual__c+1;
                                     }
                                }
                             td.actual__c=td.actual__c+1;
                         }
                     }
                     if(td.criticalEnabler__c=='Average product line'){
                      for(in_store_visit__c t2:storeTrigger){
                                     if(t.outletName_del__c==t2.outletname_del__c && t.visit_date__c.month()-1==t2.visit_date__c.month()){
                                         td.actual__c=t2.sku_number__c;
                                     }
                                }
                         td.actual__c=t.sku_number__c;
                     }
                 num++;
                 if(num==tddl.size()){
                     storeTrigger.remove(i);
                   }  
                 }
             }
         }
         
         update tddl;
         
     }
     if(storeTrigger.size()>0){
        outlet.clear();
        lastd.clear();
     for(In_Store_Visit__c t:storeTrigger){
         outlet.add(t.outletname_del__c);
     }
     for(In_Store_Visit__c t:storeTrigger){
         lastd.add(t.visit_date__c.addmonths(-1).tostartofmonth());
     }
         list<targetStandard__c> tsl=new list<targetstandard__c>();
         list<TradeDashboardDetails__c> lastmonth=new list<TradeDashboardDetails__c>();
         TargetStandard__c ts;
         TradeDashboardDetails__c tdd;

         
         lastmonth=[select id,ownerid,outlet__c,salesMonth__c,CriticalEnabler__c,ScoreStandard__c,target__c,Actual__c
                          from TradeDashboardDetails__c 
                          where ownerid=:userinfo.getuserid() and outlet__c in:outlet and SalesMonth__c in:lastd and (criticalEnabler__c=:'Coverage' or criticalEnabler__c=:'Average product line')];
          ScoreStandard__c ss1=ScoreStandard__c.getValues('Average product line');
          ScoreStandard__c ss2=ScoreStandard__c.getValues('Coverage');
         
            for(In_Store_Visit__c tri:storeTrigger){
                if(lastmonth.size()>0){    
                     for(TradeDashboardDetails__c l:lastmonth){
                         if(l.outlet__c==tri.OutletName_del__c && tri.visit_date__c.addmonths(-1).tostartofmonth()==l.salesmonth__c){
                             if(l.criticalEnabler__c=='Coverage'){
                                 if(system.trigger.isinsert){
                                 for(in_store_visit__c t2:storeTrigger){
                                     if(tri.outletName_del__c==t2.outletname_del__c && tri.visit_date__c.month()-1==t2.visit_date__c.month()){
                                         l.actual__c=l.actual__c+1;
                                     }
                                }
                                     tdd=new TradeDashboardDetails__c(criticalEnabler__c='Coverage',ScoreStandard__c=ss1.Standard__c,target__c=TargetStandard__c.getValues(tri.Channel__c).CallFrequencyStandard__c,actual__c=1,LastMonthActual__c=l.actual__c,ownerid=userinfo.getuserid(),outlet__c=tri.OutletName_del__c,salesmonth__c=tri.Visit_Date__c.toStartOfMonth());
                                     tinsert.add(tdd);
                                     
                                 }
                             }
                             if(l.criticalEnabler__c=='Average product line'){
                                  for(in_store_visit__c t2:storeTrigger){
                                     if(tri.outletName_del__c==t2.outletname_del__c && tri.visit_date__c.month()-1==t2.visit_date__c.month()){
                                         l.actual__c=t2.sku_number__c;
                                     }
                                 }
                                 tdd=new TradeDashboardDetails__c(criticalEnabler__c='Average product line',ScoreStandard__c=ss2.Standard__c,target__c=TargetStandard__c.getValues(tri.Channel__c).SKUStandard__c,actual__c=tri.sku_number__c,LastMonthActual__c=l.actual__c,ownerid=userinfo.getuserid(),outlet__c=tri.OutletName_del__c,salesmonth__c=tri.Visit_Date__c.toStartOfMonth());
                                 tinsert.add(tdd);
                             }
                         }
                     }
                 }else{
                             if(system.trigger.isinsert){
                                 tdd=new TradeDashboardDetails__c(criticalEnabler__c='Coverage',ScoreStandard__c=ss1.Standard__c,target__c=TargetStandard__c.getValues(tri.Channel__c).CallFrequencyStandard__c,actual__c=1,LastMonthActual__c=0,ownerid=userinfo.getuserid(),outlet__c=tri.OutletName_del__c,salesmonth__c=tri.Visit_Date__c.toStartOfMonth());
                                 tinsert.add(tdd);
                             }
                             tdd=new TradeDashboardDetails__c(criticalEnabler__c='Average product line',ScoreStandard__c=ss2.Standard__c,target__c=TargetStandard__c.getValues(tri.Channel__c).SKUStandard__c,actual__c=tri.sku_number__c,LastMonthActual__c=0,ownerid=userinfo.getuserid(),outlet__c=tri.OutletName_del__c,salesmonth__c=tri.Visit_Date__c.toStartOfMonth());
                             tinsert.add(tdd);
                 }
             
             }
             
 
         insert tinsert;
     }
     }
}